package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.service.SendMessageFeignService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created On : 2022/11/17 0017.
 * <p>
 * Author : shiguangchuan
 * <p>
 * Description: SendMessageController
 */
@Slf4j
@RestController
@RequestMapping("/sgc")
@Api(tags = "08、到货提醒(sgc)",value = "用户可在商品详情页面以及历史足迹的界面，将无货的商品，设置到货提醒")
public class SendMessageController {

    @Autowired
    private SendMessageFeignService sendMessageFeignService;

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/17 0017
     * @param  : [shopUser, goodsId, phoneNum]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 登记
     */
    @GetMapping("/toInsertMessage")
    @ApiOperation(value = "用户对已售罄的商品登记到货提醒",notes = "用户点击到货提醒，如果不填入号码使用用户绑定的号码接收短信，填入号码则以填入的号码为依据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "需要到货提醒登记的商品",required = true),
            @ApiImplicitParam(name = "phoneNum",value = "填入电话号码，不填则为用户绑定的号码",required = false)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<String> toInsertSendMessage(@ApiIgnore @RequestShopUser ShopUser shopUser,
                                                     @RequestParam String goodsId,
                                                     @RequestParam String phoneNum){

        // 判断是否有电话号码传入，有就用新填入的做提醒
        if (StringUtils.isNotBlank(phoneNum)){

            // 正则校验手机号
            String regex = "^((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[56])|(17[0-8])|(18[0-9])|(19[1589]))\\d{8}$";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(phoneNum);

            if (!m.matches()){
                // 手机格式不正确
                return ResultBuildUtil.fail("手机号格式不规范请重新输入！");
            }
            // 手机格式正确的情况，开始进行登记
            if (sendMessageFeignService.feignInvokeSendMessage(phoneNum,goodsId)){
                // 登记成功
                return ResultBuildUtil.success("到货提醒登记成功，补货后将发送短信提醒你！");

            }
            // 登记失败
            return ResultBuildUtil.fail("该商品货物充足，不能进行到货提醒！");

        }

        // 使用默认手机号，进行登记
        if (sendMessageFeignService.feignInvokeSendMessage(shopUser.getMobile(),goodsId)){
            // 登记成功
            return ResultBuildUtil.success("到货提醒登记成功，补货后将发送短信提醒你！");

        }
        // 登记失败
        return ResultBuildUtil.fail("该商品货物充足，不能进行到货提醒！");
        
    }

    /**
     * @author : shiguangchuan
     * @date   : 2022/11/18 0018
     * @param  : [goodsId, goodsNumber]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 加仓并发送短信提醒
     */
    @PostMapping("/addStockToSend")
    @ApiOperation(value = "模拟对商品加货",notes = "加货后，向需要接收到提醒的用户发送短信，只限本次需要的加货提醒，不会重复提醒")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "加货的商品id",required = true),
            @ApiImplicitParam(name = "goodsNumber",value = "加货数量")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<String> toInsertSendMessage(@RequestParam String goodsId,
                                                     @RequestParam Integer goodsNumber){
        
        // 调用远程业务，执行加仓并发送短信提醒（异步执行）
        sendMessageFeignService.feignInvokeAddStockToSendMessage(goodsId,goodsNumber);
        
        return ResultBuildUtil.success("加仓成功，已向需要接收短信的用户发送短信提醒！");
        
    }

}
